# -*- coding: utf-8 -*-
import os
import re
import maya.cmds as mc
from liberHook.BaseHook import BaseHook
from liberLibs.dccLibs import Maya


class Hook(BaseHook):
    def __init__(self, library, directory, ext, start, end):
        BaseHook.__init__(self, library, directory, ext, start, end)

    def execute(self):
        if not Maya.load_plugin("xgenToolkit.mll"):
            self.append_error("Load xgenToolkit.mll failed.")
            return
        if not Maya.load_plugin("AbcExport.mll"):
            self.append_error("Load AbcExport.mll failed.")
            return
        file_name = mc.file(q=1, sn=1)
        if not file_name:
            self.append_error("Save file first.")
            return
        Maya.export_xgen_archive(self.name, self.location, self.start, self.end)
        if os.path.isfile(self.file):
            with open(self.file, "r") as f:
                new_str = re.sub('thumbnail=".*\.png"', 'thumbnail="%s"' % self.thumbnail, f.read())
            with open(self.file, "w") as f:
                f.write(new_str)
